<template>
    <div>
        <h4>发表评论</h4>
        <hr>
        <textarea placeholder="please" maxlength="12" v-model="msg"></textarea>
        <mt-button type="primary" size="large" @click="postMsg">
            发表评论 
        </mt-button>

        <div class="cmt-list" >
            <div class="cmt-item" v-for="(item,i) in cmtlist" :key="i">
                <div class="cmt-item-title" >
                    第{{ i+1 }}楼&nbsp; {{ item.name }} 用户：{{ item.user_name }} &nbsp;
                    发表时间：{{ item.add_time | dateFormat }} 
                    
                    <div class="cmt-item-body"> {{item.content}}</div>
                </div>
            </div>
        </div>

        <mt-button type="primary" size="large" plain @click="loadMore">加载更多</mt-button>
    </div>
</template>

<script>
     //按需导入
    import {Toast} from 'mint-ui'

    export default{
        data(){
            return{ 
                cmtlist:[],
                page:1,
                msg:"",
            }
        },
        created(){
            this.getComment();
        },
        methods:{
            async getComment(){
                const{data} = await this.$http.get("api/getcomments/" + this.newsid + "?pageindex="+this.page)
                console.log(data)
                if(data.status === 0) 
                return (this.cmtlist = this.cmtlist.concat(data.message));
            },
            loadMore(){
                this.page++;
                this.getComment();
            },
            async postMsg(){
                if (this.msg.trim().length<=0) return Toast("请填写内容");
                //发表评论逻辑
                const{data} = await this.$http.post(
                    "api/postcomment/" + this.newsid, 
                {
                 content:this.msg.trim()
                }
                );
                 if(data.status === 0){
                     
                    //  this.getComment();
                 //不可采用清空，在客户端手动拼接出对象，unsheift方法
                    this.cmtlist.unshift({
                        user_name:"匿名用户",
                        add_time:new Date(),
                        content:this.msg.trim()
                                                    
                    });
                    
                    this.msg = "";
                 }

            }
                       
        },
        props:["newsid"]
    };


</script>

<style lang="scss" scoped>
   


    textarea{
        font-size: 13px;
        margin: 0;
    }


    .cmt-list{
        margin-top:4px;
        .cmt-item{
            line-height: 30px;
            .cmt-item-title{ 
                font-size: 14px;
                background-color:#bbb;}
            .cmt-item-body{ 
                font-size: 12px;
                text-indent:2em;
                
                }
        }
    }
</style>